<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="退换货单号" prop="returnNo">
        <el-input
          v-model="queryParams.returnNo"
          placeholder="请输入退换货单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退换货类型" prop="returnType">
        <el-select
          v-model="queryParams.returnType"
          placeholder="请选择退换货类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_RETURN_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退换货原因" prop="returnReason">
        <el-input
          v-model="queryParams.returnReason"
          placeholder="请输入退换货原因"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="原因描述" prop="returnReasonDesc">
        <el-input
          v-model="queryParams.returnReasonDesc"
          placeholder="请输入原因描述"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退换货总数量" prop="totalQuantity">
        <el-input
          v-model="queryParams.totalQuantity"
          placeholder="请输入退换货总数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退换货总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入退换货总金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="货币代码" prop="currencyCode">
        <el-input
          v-model="queryParams.currencyCode"
          placeholder="请输入货币代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退换货状态" prop="returnStatus">
        <el-select
          v-model="queryParams.returnStatus"
          placeholder="请选择退换货状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_RETURN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker
          v-model="queryParams.applyTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="审批时间" prop="approveTime">
        <el-date-picker
          v-model="queryParams.approveTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="审批人" prop="approveUser">
        <el-input
          v-model="queryParams.approveUser"
          placeholder="请输入审批人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审批备注" prop="approveRemark">
        <el-input
          v-model="queryParams.approveRemark"
          placeholder="请输入审批备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退货截止时间" prop="returnDeadline">
        <el-date-picker
          v-model="queryParams.returnDeadline"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择退货截止时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退货地址编号" prop="returnAddressId">
        <el-input
          v-model="queryParams.returnAddressId"
          placeholder="请输入退货地址编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退货配送方式" prop="returnShippingMethod">
        <el-input
          v-model="queryParams.returnShippingMethod"
          placeholder="请输入退货配送方式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退货运单号" prop="returnTrackingNo">
        <el-input
          v-model="queryParams.returnTrackingNo"
          placeholder="请输入退货运单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退货发货时间" prop="returnShipTime">
        <el-date-picker
          v-model="queryParams.returnShipTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="退货收货时间" prop="returnReceiveTime">
        <el-date-picker
          v-model="queryParams.returnReceiveTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="质检结果" prop="qualityCheckResult">
        <el-input
          v-model="queryParams.qualityCheckResult"
          placeholder="请输入质检结果"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="质检时间" prop="qualityCheckTime">
        <el-date-picker
          v-model="queryParams.qualityCheckTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="质检员" prop="qualityCheckUser">
        <el-input
          v-model="queryParams.qualityCheckUser"
          placeholder="请输入质检员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退款方式" prop="refundMethod">
        <el-input
          v-model="queryParams.refundMethod"
          placeholder="请输入退款方式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker
          v-model="queryParams.refundTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="退款交易ID" prop="refundTransactionId">
        <el-input
          v-model="queryParams.refundTransactionId"
          placeholder="请输入退款交易ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户留言" prop="customerMessage">
        <el-input
          v-model="queryParams.customerMessage"
          placeholder="请输入客户留言"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内部备注" prop="internalNotes">
        <el-input
          v-model="queryParams.internalNotes"
          placeholder="请输入内部备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="附件URL列表JSON" prop="attachments">
        <el-input
          v-model="queryParams.attachments"
          placeholder="请输入附件URL列表JSON"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crossborder:order-return:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['crossborder:order-return:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['crossborder:order-return:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="退换货编号" align="center" prop="id" />
      <el-table-column label="退换货单号" align="center" prop="returnNo" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="客户编号" align="center" prop="customerId" />
      <el-table-column label="退换货类型" align="center" prop="returnType" />
      <el-table-column label="退换货原因" align="center" prop="returnReason" />
      <el-table-column label="原因描述" align="center" prop="returnReasonDesc" />
      <el-table-column label="退换货总数量" align="center" prop="totalQuantity" />
      <el-table-column label="退换货总金额" align="center" prop="totalAmount" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="货币代码" align="center" prop="currencyCode" />
      <el-table-column label="退换货状态" align="center" prop="returnStatus" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="applyTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="审批时间"
        align="center"
        prop="approveTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="审批人" align="center" prop="approveUser" />
      <el-table-column label="审批备注" align="center" prop="approveRemark" />
      <el-table-column
        label="退货截止时间"
        align="center"
        prop="returnDeadline"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="退货地址编号" align="center" prop="returnAddressId" />
      <el-table-column label="退货配送方式" align="center" prop="returnShippingMethod" />
      <el-table-column label="退货运单号" align="center" prop="returnTrackingNo" />
      <el-table-column
        label="退货发货时间"
        align="center"
        prop="returnShipTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="退货收货时间"
        align="center"
        prop="returnReceiveTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="质检结果" align="center" prop="qualityCheckResult" />
      <el-table-column
        label="质检时间"
        align="center"
        prop="qualityCheckTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="质检员" align="center" prop="qualityCheckUser" />
      <el-table-column label="退款方式" align="center" prop="refundMethod" />
      <el-table-column
        label="退款时间"
        align="center"
        prop="refundTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="退款交易ID" align="center" prop="refundTransactionId" />
      <el-table-column label="客户留言" align="center" prop="customerMessage" />
      <el-table-column label="内部备注" align="center" prop="internalNotes" />
      <el-table-column label="附件URL列表JSON" align="center" prop="attachments" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crossborder:order-return:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crossborder:order-return:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrderReturnForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderReturnApi, OrderReturn } from '@/api/crossborder/orderreturn'
import OrderReturnForm from './OrderReturnForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 跨境电商退换货申请 列表 */
defineOptions({ name: 'OrderReturn' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrderReturn[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  returnNo: undefined,
  orderId: undefined,
  orderNo: undefined,
  customerId: undefined,
  returnType: undefined,
  returnReason: undefined,
  returnReasonDesc: undefined,
  totalQuantity: undefined,
  totalAmount: undefined,
  refundAmount: undefined,
  currencyCode: undefined,
  returnStatus: undefined,
  applyTime: [],
  approveTime: [],
  approveUser: undefined,
  approveRemark: undefined,
  returnDeadline: undefined,
  returnDeadline: [],
  returnAddressId: undefined,
  returnShippingMethod: undefined,
  returnTrackingNo: undefined,
  returnShipTime: [],
  returnReceiveTime: [],
  qualityCheckResult: undefined,
  qualityCheckTime: [],
  qualityCheckUser: undefined,
  refundMethod: undefined,
  refundTime: [],
  refundTransactionId: undefined,
  customerMessage: undefined,
  internalNotes: undefined,
  attachments: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderReturnApi.getOrderReturnPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrderReturnApi.deleteOrderReturn(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除跨境电商退换货申请 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await OrderReturnApi.deleteOrderReturnList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: OrderReturn[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderReturnApi.exportOrderReturn(queryParams)
    download.excel(data, '跨境电商退换货申请.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>